<template>
  <div>
    <div v-if="!isLogin" class="c-top">
      <div class="c-t-left">
        <h1>吾志是一个简单、私密的日记本</h1>
        <p>
          <span>私密</span>
          ：完全私密的个人空间，在这里你可以安心的记录最真实的自己。
        </p>
        <p><span>简单</span> ：去繁就简，专注于提供简洁、纯粹的日记功能。</p>
      </div>
      <div class="c-t-right">
        <div class="form">
          <div>
            <input @click="myRegister" type="submit" value="立即注册" />
          </div>
          <div>
            <span>or</span>
          </div>
          <div>
            <input @click="myLogin" type="submit" value="登录" />
          </div>
        </div>
      </div>
    </div>
    <div v-if="!isLogin" class="c-center">
      <ul>
        <li v-for="item in homePageArray" :key="item.id">
          <a href="javascript:;"
            ><img @click="goLogin" :src="item[0].imgurl" alt=""
          /></a>
        </li>
      </ul>
    </div>
    <div v-if="isLogin" class="myrecent">
      <h2>最近发表的日记</h2>
      <div class="recent">
        <div class="r-left">
          <div
            v-for="(item, index) in bigDiaryArray"
            :key="index"
            class="big-month-box"
          >
            <div class="month-box">
              <div class="month">
                <div class="top">{{ bigDiaryArray[index][0].month }} 月</div>
                <div class="bottom">{{ bigDiaryArray[index][0].day }}</div>
              </div>
            </div>
            <div class="diary-box">
              <ul>
                <li v-for="item in bigDiaryArray[index]" :key="item.id">
                  <p>{{ item.time }}</p>
                  <p>{{ item.content }}</p>
                </li>
              </ul>
            </div>
          </div>
          <div class="tip" v-if="isNull">
            <p>您还没有发表过任何日记...</p>
          </div>
        </div>
        <div class="r-right">
          <div class="qcode">
            <p>关注吾志微信账号，用微信写日记</p>
            <img src="https://wuzhi.me/img/wuzhi_weixin_qrcode.jpg" alt="" />
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { mapState } from "vuex";

export default {
  name: "webHomepage",
  data() {
    return {
      homePageArray: [],
      isNull: true,
    };
  },
  methods: {
    myRegister() {
      this.$router.push({ path: "/home/reg" });
    },
    myLogin() {
      this.$router.push({ path: "/home/login" });
    },
    goLogin() {
      alert("请登录！");
      this.$store.state.loginStr = "请登录";
      this.$store.state.noLogin = true;
      this.$router.push("/home/login");
    },
  },
  mounted() {
    let dt = new Date();
    let year = dt.getFullYear();
    let mm = dt.getMonth() + 1;
    let dd = dt.getDate();
    let h = dt.getHours();
    let m = dt.getMinutes();
    let weekend = dt.getDay();

    mm = mm < 10 ? "0" + mm : mm;
    dd = dd < 10 ? "0" + dd : dd;
    h = h < 10 ? "0" + h : h;
    m = m < 10 ? "0" + m : m;
    if (weekend == 1) {
      weekend = "一";
    } else if (weekend == 2) {
      weekend = "二";
    } else if (weekend == 3) {
      weekend = "三";
    } else if (weekend == 4) {
      weekend = "四";
    } else if (weekend == 5) {
      weekend = "五";
    } else if (weekend == 6) {
      weekend = "六";
    } else if (weekend == 7) {
      weekend = "日";
    }
    let obj = {year: year, month: mm, day: dd, weekend: weekend, hour: h, minute: m }
    this.$store.state.timeObj = obj;

    let len = this.$store.state.bigDiaryArray.length;
    if (len != 0) {
      this.isNull = false;
    } else {
      this.isNull = true;
    }
    // 测试月份
    // console.log(this.$store.state.bigDiaryArray[0][0].month);
    // console.log(myRandom);
    axios.get("imgUrl.json").then((res) => {
      // console.log(res.data);
      let arr = res.data;
      let len = arr.length;

      for (let i = 0; i < 5; i++) {
        let myRandom = Math.floor(Math.random() * len);
        // 去重
        let obj = arr.splice(myRandom, 1);
        // console.log(obj);
        this.homePageArray.push(obj);
      }
    });
  },
  computed: {
    // ...mapState(["isLogin",'bigDiaryArray','bigDiaryTime']),
    ...mapState(["isLogin", "bigDiaryArray"]),
  },
};
</script>

<style lang="less" scoped>
.c-top {
  width: 100%;
  height: 150px;
  display: grid;
  grid-template-rows: 150px;
  grid-template-columns: 2fr 1fr;
  padding-top: 30px;
  .c-t-left {
    padding-top: 10px;
    box-sizing: border-box;
    h1 {
      margin-bottom: 30px;
    }
    p {
      font-size: 16px;
      &:nth-of-type(1) {
        margin: 22px 0;
      }
      span {
        color: #00995b;
        font-weight: bold;
      }
    }
  }
  .c-t-right {
    border-left: 1px solid #000;
    .form {
      width: 100%;
      padding-top: 20px;
      box-sizing: border-box;
      div {
        width: 100%;
        text-align: center;
        input {
          font-size: 16px;
          color: #f0f0f0;
          background-color: #9fbb58;
          border-radius: 5px;
          padding: 10px 25px;
          cursor: pointer;
          &:hover {
            background-color: #8ba644;
          }
        }
        span {
          display: block;
          margin: 10px 0;
          color: #808080;
          font-size: 15px;
          font-weight: bold;
        }
      }
    }
  }
}
.c-center {
  width: 100%;
  padding-top: 20px;
  box-sizing: border-box;
  ul {
    width: 100%;
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex-wrap: nowrap;
    li {
      width: 150px;
      height: 150px;
      padding: 2px 2px;
      // box-sizing: border-box;
      background-color: #f0f0f0;
      a {
        display: block;
        box-shadow: 1px 1px 5px 2px #ccc;
        width: 100%;
        height: 100%;
        padding: 4px 4px;
        box-sizing: border-box;
        background-color: #f0f0f0;
        img {
          width: 100%;
          height: 100%;
        }
      }
    }
  }
}
.myrecent {
  width: 100%;
  h2 {
    font-size: 25px;
    margin: 15px 0;
  }
  .recent {
    width: 100%;
    display: flex;
    justify-content: space-between;
    > div {
      // height: 300px;
    }
    .r-left {
      width: 570px;
      position: relative;
      .tip {
        position: absolute;
        top: 50%;
        left: 50%;
        margin-left: -150px;
        margin-top: -20px;
        p {
          color: #808080;
          font-size: 25px;
        }
      }
      .big-month-box {
        margin-bottom: 15px;
        width: 100%;
        display: flex;
        .month-box {
          width: 65px;
          .month {
            width: 45px;
            height: 45px;
            border-radius: 7px;
            overflow: hidden;
            box-shadow: 1px 1px 2px 1px #ccc;
            .top {
              height: 20px;
              color: #fff;
              text-align: center;
              background-color: #34495e;
              font-size: 14px;
              line-height: 25px;
            }
            .bottom {
              text-align: center;
              font-size: 22px;
              color: #34495e;
              background-color: #fff;
              height: 25px;
            }
          }
        }
        .diary-box {
          // float: left;
          border: 1px solid #ccc;
          border-radius: 5px;
          width: 500px;
          ul {
            padding: 0 15px;
            box-sizing: border-box;
            li {
              margin: 15px 0;
              p {
                font-size: 16px;
                &:nth-of-type(1) {
                  color: #808080;
                  margin-bottom: 5px;
                }
                &:nth-of-type(2) {
                  padding-left: 15px;
                }
              }
            }
          }
        }
      }
    }
    .r-right {
      width: 250px;
      // background-color: #ccc;
      margin-top: -20px;
      .calendar {
        height: 300px;
        .time-box {
          display: flex;
          justify-content: space-between;
        }
      }
      .qcode {
        width: 100%;
        p {
          text-align: center;
        }
        img {
          width: 100%;
        }
      }
    }
  }
}
</style>